package com.xjg.dynconfig;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.xjg.framework.common.zookeeper.ZookeeperFactoryBean;
@Component
public class PushConfigs {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private ZookeeperFactoryBean zookeeperFactoryBean;
	public static Map<String,Object> pushConfigs;
	public String path = "/zookeeper/xjg/push/config";
	
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	
//	public PushConfigs(){
//		init();
//	}
	private void init(){
		try {
			byte[] bytes = zookeeperFactoryBean.getObject().getData().forPath(path);
			pushConfigs = JSON.parseObject(bytes, Map.class);
			logger.info("pushConfigs:"+pushConfigs);
		} catch (Exception e) {
			logger.error("apiconfigs data init fail",e);
		}
	}
	public Map<String,Object> getPushConfigs(){
		if(pushConfigs==null){
			init();
		}
		
		return pushConfigs;
	}
	
	public String getQueueByServer(String server){
		Map<String,String> queue = (Map<String,String>)getPushConfigs().get("queue");
		
		return queue.get(server)==null?"com.xjg.mtq.push1":queue.get(server);
	}
}
